package com.mengtian_tech.tju.dataquery.api.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.util.StringUtil;
import com.mengtian_tech.tju.dataquery.api.model.QueryType;
import com.mengtian_tech.tju.dataquery.api.model.tjuOracle.College;
import com.mengtian_tech.tju.dataquery.api.service.AskLeaveService;
import com.mengtian_tech.tju.dataquery.api.service.AsyncDateService;
import com.mengtian_tech.tju.dataquery.api.service.EmployeeService;
import com.mengtian_tech.tju.dataquery.api.service.QueryCarService;
import com.mengtian_tech.tju.dataquery.api.service.QueryLogService;
import com.mengtian_tech.tju.dataquery.api.service.QueryPersonService;
import com.mengtian_tech.tju.dataquery.api.service.SchoolLeaderService;
import com.mengtian_tech.tju.dataquery.api.util.ResponseUtil;

/**
 * 查询控制器
 * 
 * @author wxd56
 *
 */
@RestController
@RequestMapping("/api/query")
public class QueryController {

	@Autowired
	QueryCarService queryCarService;

	@Autowired
	QueryPersonService queryPersonService;

	@Autowired
	AskLeaveService askLeaveService;

	@Autowired
	QueryLogService queryLogService;

	@Autowired
	AsyncDateService asyncDateService;

	@Autowired
	SchoolLeaderService schoolLeaderService;
	@Autowired
	EmployeeService employeeService;

	/**
	 * 首页综合查询
	 * 
	 * @param key
	 * @param pageNo
	 */
	@RequestMapping("queryAll")
	public Object queryAll(String key, String postLevel, String taskType, HttpServletRequest req)
			throws ParseException {

		// 返回内容
		Map<String, Object> result = new HashMap<String, Object>();
		if (key != null)
			key = key.trim();

		// 记录日志
		this.queryLogService.addLog(req.getRemoteUser(), QueryType.TOP_SEARCH, key, "首页搜索");

		// 判断是关键词
		if ("今日干部请假情况".equals(key)) {
			List<Map<String, Object>> list = this.askLeaveService.getTodayList(postLevel, taskType);
			result.put("leaveList", list);
			return ResponseUtil.ok(result);
		}

		// 首先判断搜索的是否为人
		List<Map<String, Object>> temp = this.queryPersonService.queryUserByName(key);
		if (temp.size() > 0) {
			result.put("personList", temp);
		}

		// 然后搜索车辆信息
		if (temp.size() == 0) {
			key = key.toUpperCase();
			temp = this.queryCarService.queryCardInfo(key, 1, 50);
			List<Map<String, Object>> singleCarList = getSingleCarList(temp, key);
			result.put("carList", singleCarList);
		}

		return ResponseUtil.ok(result);

	}

	/**
	 * 根据姓名或者车牌号查找车辆备案、出入记录信息
	 * 
	 * @param plateNo
	 * @param name
	 */
	@RequestMapping("queryCar")
	public Object queryCar(String key, Integer pageNo, HttpServletRequest req, Integer query1, Integer query2,
			Integer query3,String beginDate,String endDate) throws Exception{
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		if (pageNo == null)
			pageNo = 1;

		// 记录日志
		this.queryLogService.addLog(req.getRemoteUser(), QueryType.CAR_QUERY, key, "车辆查询");

		// 备案信息
		List<Map<String, Object>> carInfoList = new ArrayList<Map<String, Object>>();

		// 返回内容
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("carInfoList", carInfoList);

		// 最近同步日期
		Date asynDate = asyncDateService.getAsyncDate("01");
		result.put("asynDate", asynDate);
		

		// 首先查询备案信息
		List<Map<String, Object>> temp = null;

		// 参数为空，直接返回
		if (StringUtil.isNotEmpty(key)) {
			key = key.trim();

			if (isChinese(key)) {
				temp = this.queryCarService.queryCarInfoByName(key, pageNo);
			} else {
				key = key.toUpperCase();
				temp = this.queryCarService.queryCardInfo(key, pageNo, 20);
			}

		} else {
			
			Date begin = null;
			Date end = null;
			
			if(StringUtil.isNotEmpty(beginDate)) begin = sdf.parse(beginDate);
			if(StringUtil.isNotEmpty(endDate)) end = sdf.parse(endDate);
			
			temp = this.queryCarService.getCarInfoList(pageNo, query1, query2, query3,begin,end);
		}
		
		//过滤教职工车辆的单位
		for(Map<String, Object> t: temp) {
			if("4".equals(t.get("apply_type").toString())) {//教职工车辆
				String staffId = t.get("GZH").toString();
				College c =  this.employeeService.getCollege(staffId);
				if(c != null)t.put("DW", c.getCollegeName());
				
			}
		}
		

		// 多个车辆，进行数据拆分,并且只过滤查询条件中的车牌
		List<Map<String, Object>> singleCarList = getSingleCarList(temp, key);
		carInfoList.addAll(singleCarList);

		// 查询出行记录
		/*if (temp.size() == 1) {
			for (Map<String, Object> car : carInfoList) {
				String no = (String) car.get("CPH");

				List<Map<String, Object>> carInfoParkList = new ArrayList<Map<String, Object>>();

				if (StringUtil.isNotEmpty(no)) {
					List<Map<String, Object>> list = this.queryCarService.queryParkLog(no);
					carInfoParkList.addAll(list);
				}
				car.put("parkList", carInfoParkList);
			}
		}*/

		return ResponseUtil.ok(result);

	}

	private List<Map<String, Object>> getSingleCarList(List<Map<String, Object>> temp, String key) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		for (Map<String, Object> car : temp) {

			String no = (String) car.get("CPH");
			String no1 = (String) car.get("CPHM");
			String no2 = (String) car.get("CPHM1");
			String no3 = (String) car.get("CPHM2");
			String no4 = (String) car.get("CPHM3");
			String no5 = (String) car.get("CPHM4");
			String no6 = (String) car.get("CPHM5");

			if (key == null || isChinese(key)) {// 查询姓名，拆分所有
				if (StringUtil.isNotEmpty(no)) {
					Map<String, Object> theItem = new HashMap<String, Object>(car);
					theItem.put("CPH", no);
					list.add(theItem);
				}
				if (StringUtil.isNotEmpty(no1)) {
					Map<String, Object> theItem = new HashMap<String, Object>(car);
					theItem.put("CPH", no1);
					list.add(theItem);
				}
				if (StringUtil.isNotEmpty(no2)) {
					Map<String, Object> theItem = new HashMap<String, Object>(car);
					theItem.put("CPH", no2);
					list.add(theItem);
				}
				if (StringUtil.isNotEmpty(no3)) {
					Map<String, Object> theItem = new HashMap<String, Object>(car);
					theItem.put("CPH", no3);
					list.add(theItem);
				}
				if (StringUtil.isNotEmpty(no4)) {
					Map<String, Object> theItem = new HashMap<String, Object>(car);
					theItem.put("CPH", no4);
					list.add(theItem);
				}
				if (StringUtil.isNotEmpty(no5)) {
					Map<String, Object> theItem = new HashMap<String, Object>(car);
					theItem.put("CPH", no5);
					list.add(theItem);
				}
				if (StringUtil.isNotEmpty(no6)) {
					Map<String, Object> theItem = new HashMap<String, Object>(car);
					theItem.put("CPH", no6);
					list.add(theItem);
				}

			} else { // 只拆分包含车牌的车辆

				if (StringUtil.isNotEmpty(no) && no.contains(key)) {
					Map<String, Object> theItem = new HashMap<String, Object>(car);
					theItem.put("CPH", no);
					list.add(theItem);
				}
				if (StringUtil.isNotEmpty(no1) && no1.contains(key)) {
					Map<String, Object> theItem = new HashMap<String, Object>(car);
					theItem.put("CPH", no1);
					list.add(theItem);
				}
				if (StringUtil.isNotEmpty(no2) && no2.contains(key)) {
					Map<String, Object> theItem = new HashMap<String, Object>(car);
					theItem.put("CPH", no2);
					list.add(theItem);
				}
				if (StringUtil.isNotEmpty(no3) && no3.contains(key)) {
					Map<String, Object> theItem = new HashMap<String, Object>(car);
					theItem.put("CPH", no3);
					list.add(theItem);
				}
				if (StringUtil.isNotEmpty(no4) && no4.contains(key)) {
					Map<String, Object> theItem = new HashMap<String, Object>(car);
					theItem.put("CPH", no4);
					list.add(theItem);
				}
				if (StringUtil.isNotEmpty(no5) && no5.contains(key)) {
					Map<String, Object> theItem = new HashMap<String, Object>(car);
					theItem.put("CPH", no5);
					list.add(theItem);
				}
				if (StringUtil.isNotEmpty(no6) && no6.contains(key)) {
					Map<String, Object> theItem = new HashMap<String, Object>(car);
					theItem.put("CPH", no6);
					list.add(theItem);
				}

			}
		}

		return list;
	}

	public static boolean isChinese(String str) {
		// 匹配全部为中文的正则表达式
		String regex = "^[\\u4e00-\\u9fa5]+$";
		return Pattern.matches(regex, str);
	}

	@RequestMapping("queryCarPark")
	public Object queryCarPark(Long id, Integer pageNo, String plateNo, HttpServletRequest req) {

		if (pageNo == null)
			pageNo = 1;

		// 记录日志
		this.queryLogService.addLog(req.getRemoteUser(), QueryType.CAR_QUERY, plateNo, "车辆停车记录查询");

		// 返回内容
		Map<String, Object> result = new HashMap<String, Object>();

		// 备案信息
		if (id != null) {
			Map<String, Object> carInfo = this.queryCarService.selectCarById(id);
			result.put("carInfo", carInfo);
		}

		// 出入记录
		if (plateNo != null) {
			List<Map<String, Object>> list = this.queryCarService.queryParkLog(plateNo, pageNo);
			result.put("list", list);
		}

		Date asynDate = asyncDateService.getAsyncDate("04");
		result.put("asynDate", asynDate);

		return ResponseUtil.ok(result);

	}

	@RequestMapping("queryCarParkList")
	public Object queryCarParkList(Integer pageNo, String school, String begin, String end) {

		if (pageNo == null)
			pageNo = 1;

		// 返回内容
		Map<String, Object> result = new HashMap<String, Object>();

		List<Map<String, Object>> list = this.queryCarService.queryParkLog(school, begin, end, pageNo);
		result.put("list", list);

		Date asynDate = asyncDateService.getAsyncDate("04");
		result.put("asynDate", asynDate);

		return ResponseUtil.ok(result);

	}

	/**
	 * 查询人员信息
	 */
	@RequestMapping("queryPerson")
	public Object queryPerson(String key, Integer pageNo, HttpServletRequest req, String query1, String query2,
			String query3) {

		if (pageNo == null)
			pageNo = 1;

		// 记录日志
		this.queryLogService.addLog(req.getRemoteUser(), QueryType.PERSON_QUERY, key, "查询人员信息");

		// 返回内容
		Map<String, Object> result = new HashMap<String, Object>();

		// 参数为空，直接返回
		if (key != null) {
			key = key.trim();
		}

		// 首先查询备案信息
		List<Map<String, Object>> temp = null;
		temp = this.queryPersonService.queryUser(pageNo, key, query1, query2, query3);
		result.put("personList", temp);

		Date asynDate = asyncDateService.getAsyncDate("03");
		result.put("asynDate", asynDate);

		return ResponseUtil.ok(result);
	}

	/**
	 * 查询人员信息
	 */
	@RequestMapping("queryPersonGate")
	public Object queryPersonGate(String staffId, Integer pageNo, HttpServletRequest req) {

		if (pageNo == null)
			pageNo = 1;

		// 记录日志
		this.queryLogService.addLog(req.getRemoteUser(), QueryType.PERSON_GATE_QUERY, staffId, "查询人员进学校");

		// 返回内容
		Map<String, Object> result = new HashMap<String, Object>();

		// 首先查询人员
		Map<String, Object> temp = this.queryPersonService.queryOneUser(staffId);
		result.put("person", temp);

		List<Map<String, Object>> list = this.queryPersonService.queryVisitLog(staffId, pageNo);
		result.put("list", list);

		Date asynDate = this.queryPersonService.getAsyncDate();
		result.put("asynDate", asynDate);

		return ResponseUtil.ok(result);
	}

	@RequestMapping("queryPersonList")
	public Object queryPersonList(Integer pageNo, String school, String gate, String begin, String end) {

		if (pageNo == null)
			pageNo = 1;

		// 返回内容
		Map<String, Object> result = new HashMap<String, Object>();

		List<Map<String, Object>> list = this.queryPersonService.queryAllVisitList(pageNo, school, gate, begin, end);
		result.put("list", list);

		Date asynDate = this.queryPersonService.getAsyncDate();
		result.put("asynDate", asynDate);

		return ResponseUtil.ok(result);

	}

	/**
	 * 根据工号查询备案
	 */
	@RequestMapping("queryCarByStaffId")
	public Object queryCarByStaffId(String key, HttpServletRequest req) {

		// 返回内容
		Map<String, Object> result = new HashMap<String, Object>();

		// 记录日志
		this.queryLogService.addLog(req.getRemoteUser(), QueryType.CAR_QUERY, key, "根据工号查询备案");

		// 首先查询备案信息
		List<Map<String, Object>> temp = this.queryCarService.queryCarByStaffId(key);

		// 多个车辆，进行数据拆分,并且只过滤查询条件中的车牌
		List<Map<String, Object>> singleCarList = getSingleCarList(temp, null);
		result.put("carInfoList", singleCarList);

		// 最近同步日期
		Date asynDate = this.queryCarService.getCarAsyncDate();
		result.put("asynDate", asynDate);

		return ResponseUtil.ok(result);
	}

	@PostMapping("uploadImg")
	public Object uploadImg(@RequestParam("file") MultipartFile file) {
		System.out.println(file.getSize());
		return ResponseUtil.ok();
	}

	@RequestMapping("getSchoolLeaders")
	public Object getSchoolLeaders() {
		List<String> list = this.schoolLeaderService.getSchoolLeaderIds();
		return ResponseUtil.ok(list);
	}
}
